﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Ledict.ConditionExtract.Common
{
    public class LdFormat
    {
        /// <summary>
        /// 对字符串进行格式化
        /// </summary>
        /// <param name="obj">格式化对象</param>
        /// <returns>返回字符串</returns>
        public static string ObjectToString(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return string.Empty;
            }
            return obj.ToString();
        }

        /// <summary>
        /// 对字符串进行小写格式化
        /// </summary>
        /// <param name="obj">格式化对象</param>
        /// <returns>返回字符串</returns>
        public static string ObjectToLowString(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return string.Empty;
            }
            return obj.ToString().Trim().ToLower();
        }

        /// <summary>
        /// 把日期值转化为短日期整数；如20070809
        /// </summary>
        /// <param name="oTime">被转换值</param>
        /// <returns>转换后的值</returns>
        public static int DateTimeToShortDateInt(DateTime oTime)
        {
            return Convert.ToInt32(oTime.Year.ToString()
                    + oTime.Month.ToString().PadLeft(2, '0')
                    + oTime.Day.ToString().PadLeft(2, '0'));
        }

        /// <summary>
        /// 对字符串，转换为整形
        /// </summary>
        /// <param name="str">待转换字符串</param>
        /// <returns>转换后的整数值</returns>
        public static int StringToInt(string str)
        {
            int reInt;

            return (int.TryParse(str, out reInt)) ? reInt : 0;
        }

        /// <summary>
        ///  把对象转换为整形
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>转换后的整数值</returns>
        public static int ObjectToInt(object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception err)
            {
                err.ToString();
                return 0;
            }
        }

        /// <summary>
        /// 日期整数转换为格式化日期字符串
        /// </summary>
        /// <param name="dateInt">六位日期整数,如：20141001</param>
        /// <returns>短日期字符串，如：2014-10-01</returns>
        public static string DateIntToDateString(int dateInt)
        {
            return LdFormat.DateIntToDateString(dateInt.ToString());
        }

        /// <summary>
        /// 日期整数字符串转换为格式化日期字符串
        /// </summary>
        /// <param name="dateInt">六位日期字符串,如：20141001</param>
        /// <returns>短日期字符串，如：2014-10-01</returns>
        public static string DateIntToDateString(string dateIntStr)
        {
            if (dateIntStr.Length == 6)
            {
                return dateIntStr.Insert(4, "-").Insert(7, "-");
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 排序格式化
        /// 王宇 
        /// 2016-03-23
        /// </summary>
        /// <param name="order">1</param>
        /// <returns>0001</returns>
        public static string OrderFormat(int order)
        {
            string sResult = string.Empty;
            sResult = Convert.ToString(order);
            int count = 4 - sResult.Length;
            if (4 > sResult.Length)
            {
                for (int i = 0; i < count; i++)
                {
                    sResult = string.Format("0{0}", sResult);
                }
            }
            return sResult;
        }

        /// <summary>
        /// 排序反格式化
        /// 王宇 
        /// 2016-03-23
        /// </summary>
        /// <param name="order">0001</param>
        /// <returns>1</returns>
        public static int OrderFormat(string str)
        {
            return Convert.ToInt32(str);
        }

        /// <summary>
        /// 根据层修改排序
        /// 王宇 
        /// 2016-03-23
        /// </summary>
        /// str 排序字段  00010002
        /// value 修改值  3
        /// layer 层    2
        /// <returns>00010003</returns>
        public static string OrderEdit(string str,int value,int layer)
        {
            string sResult = string.Empty;
            MatchCollection match = Regex.Matches(str, "[\\d]{4}");
            for (int i = 0; i < match.Count; i++)
            {
                Match item = match[i];
                sResult = string.Format("{0}{1}", 
                    sResult,
                    layer == i + 1 ? LdFormat.OrderFormat(value) : item.Value);
            }
            return sResult;
        }

        /// <summary>
        /// 根据层获取排序
        /// 王宇 
        /// 2016-03-23
        /// </summary>
        /// str 排序字段  00010002
        /// layer 层    2
        /// <returns>2</returns>
        public static int OrderGet(string str, int layer)
        {
            MatchCollection match = Regex.Matches(str, "[\\d]{4}");
            for (int i = 0; i < match.Count; i++)
            {
                Match item = match[i];
                if (layer == i + 1)
                {
                    return LdFormat.OrderFormat(item.Value);
                }
            }
            return -1;
        }

    }
}
